<?php

namespace app\common\service;

use app\common\exception\WxappException;
use app\common\model\Order;
use app\common\model\Order as OrderModel;
use app\common\model\StudyPlan as StudyPlanModel;
use app\common\model\Topic as TopicModel;
use app\common\model\VideoList as VideoListModel;
use app\common\model\Vip as VipModel;
use app\common\model\WechatUser;
use think\Collection;

class OrderService
{

    /**
     * 下单 三种类型 买课程  买专栏  买vip
     * 接受参数 返回支付数据
     * @param $uid
     * @param $srcId
     * @param $type 1 video 2 topic 3 vip
     * @return mixed
     */
    public function order($uid, $srcId, $type)
    {
        //判断用户是否已经买了
        $order = null;
        switch ($type) {
            case OrderModel::VIDEO:
                $order = $this->videoOrder($uid, $srcId);
                break;
            case OrderModel::TOPIC:
                $order = $this->topicOrder($uid, $srcId);
                break;
            case OrderModel::VIP:
                $order = $this->vipOrder($uid, $srcId);
                break;
            default:
                return null;
        }

        $user = WechatUser::where(['id' => $uid])->find();
        $wxPayService = new WxPayService();
        $result = $wxPayService->wxMpPay($user['openid'], $order['fee'], $order['order_sn'], $order['body']);
        return $result;
    }

    /**
     * @param $uid
     * @param $srcId
     * @return OrderModel|\think\Model
     * @throws WxappException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function videoOrder($uid, $srcId)
    {
        //视频id
        $data = VideoListModel::where(['id' => $srcId])->find();
        if (empty($data)) {
            throw new WxappException('资源不存在');
        }
        $order = [];
        $order['src_id'] = $srcId;
        $order['uid'] = $uid;
        $order['type'] = OrderModel::VIDEO;
        $order['order_sn'] = OrderModel::createSN();
        $order['body'] = '视频-' . $data['title'];
        $order['fee'] = floatval($data['fee']) * 100;
        $order['status'] = 0;
        return OrderModel::create($order);
    }

    /**
     * @param $uid
     * @param $srcId
     * @return OrderModel|\think\Model
     * @throws WxappException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function topicOrder($uid, $srcId)
    {
        $data = TopicModel::where(['id' => $srcId])->find();
        if (empty($data)) {
            throw new WxappException('资源不存在');
        }
        $exists = StudyPlanModel::where(['uid' => $uid, 'type' => StudyPlanModel::TOPIC, 'src_id' => $srcId])->find();
        if ($exists) {
            throw new WxappException('已经加入学习计划了');
        }

        $order = [];
        $order['src_id'] = $srcId;
        $order['uid'] = $uid;
        $order['type'] = OrderModel::TOPIC;
        $order['order_sn'] = OrderModel::createSN();
        $order['body'] = '专栏-' . $data['title'];
        $order['fee'] = floatval($data['fee']) * 100;
        $order['status'] = 0;

        return OrderModel::create($order);
    }

    /**
     * 判断视频是否可以看
     * @param $uid
     * @param $type
     * @param $srcId
     * @param $videoMoney
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkTopicVideo($uid, $type, $srcId, $videoMoney)
    {
        $where = [
            'uid' => $uid,
            'status' => 1,
            'type' => $type,
            'src_id' => $srcId
        ];
        $result = Order::where($where)->field('fee,id')->select();
        if (!$result) {
            return false;
        }
        $sumMoney = 0;
        $res = $result->toArray();
        foreach ($res as $item) {
            $sumMoney += (float)$item['fee'];
        }
        if ($sumMoney >= (float)$videoMoney) {
            return true;
        }
        return false;
    }

    /**
     * @param $uid
     * @param $srcId
     * @return OrderModel|\think\Model
     * @throws WxappException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function vipOrder($uid, $srcId)
    {
        $data = VipModel::where(['id' => $srcId])->find();
        if (empty($data)) {
            throw new WxappException('资源不存在');
        }
        $order = [];
        $order['src_id'] = $srcId;
        $order['uid'] = $uid;
        $order['type'] = OrderModel::VIP;
        $order['order_sn'] = OrderModel::createSN();
        $order['body'] = 'vip-' . $data['title'];
        $order['fee'] = floatval($data['fee']) * 100;
        $order['status'] = 0;

        return OrderModel::create($order);
    }
}